package com.example.wifi_ip_info;

import android.content.Context;
import android.net.DhcpInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import androidx.annotation.NonNull;
import java.util.HashMap;
import java.util.Map;
import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;

// 1. 实现 FlutterPlugin 接口
public class WifiIpInfoPlugin implements FlutterPlugin, MethodCallHandler {
    private MethodChannel channel;
    private Context applicationContext;

    // 2. onAttachedToEngine: 这是新的插件入口点
    @Override
    public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBinding) {
        // 从 binding 中获取 Context 和 BinaryMessenger
        applicationContext = flutterPluginBinding.getApplicationContext();
        channel = new MethodChannel(flutterPluginBinding.getBinaryMessenger(), "wifi_ip_info");
        // 将 this (插件实例) 设置为 MethodCall 的处理器
        channel.setMethodCallHandler(this);
    }

    // 3. onMethodCall: 这里的逻辑和旧代码完全一样，只是 context 的来源变了
    @Override
    public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
        if (call.method.equals("getWifiIp")) {
            // 获取 WifiManager
            WifiManager wifiManager = (WifiManager) applicationContext.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
            if (wifiManager == null) {
                result.error("UNAVAILABLE", "WifiManager not available.", null);
                return;
            }
            
            // 核心功能代码，原封不动地从旧代码中复制过来
            DhcpInfo dhcpInfo = wifiManager.getDhcpInfo();
            WifiInfo wifiInfo = wifiManager.getConnectionInfo();
            
            Map<String, Object> map = new HashMap<>();
            map.put("ip", intToIp(dhcpInfo.ipAddress)); // 注意：字段名保持和旧Dart代码一致！
            map.put("netmask", intToIp(dhcpInfo.netmask));
            map.put("broadcastIP", intToIp((dhcpInfo.ipAddress & dhcpInfo.netmask) | ~dhcpInfo.netmask));
            map.put("gateway", intToIp(dhcpInfo.gateway));
            
            String ssid = wifiInfo.getSSID();
            if (ssid != null) {
                if (ssid.startsWith("\"") && ssid.endsWith("\"")) {
                    ssid = ssid.substring(1, ssid.length() - 1);
                }
            }
            map.put("ssid", ssid);

            result.success(map);
        } else {
            result.notImplemented();
        }
    }

    // 4. onDetachedFromEngine: 清理资源
    @Override
    public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
        channel.setMethodCallHandler(null);
        applicationContext = null;
    }

    // 辅助方法，原封不动地从旧代码中复制过来
    private String intToIp(int i) {
        return (i & 0xFF) + "." +
                ((i >> 8) & 0xFF) + "." +
                ((i >> 16) & 0xFF) + "." +
                (i >> 24 & 0xFF);
    }
}